<template>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>权限管理</el-breadcrumb-item>
  <el-breadcrumb-item>角色列表</el-breadcrumb-item>
</el-breadcrumb> <br>
<el-card>
   <el-button type="primary" @click="add">添加角色</el-button> <br><br>
    <el-table  :data="tableData"   border style="width: 100%">
    <el-table-column type="expand"> 
           <template slot-scope="scope">
              <div class="role_box">
                <div v-if="scope.row.children.length > 0"> 
                  <el-row
                    v-for="item1 in scope.row.children"
                    :key="item1.id"
                    class="row_box1"
                  >
                    <!-- 第一列 -->
                    <el-col :span="4">
                      <el-tag
                        type="primary"
                        closable
                        @close="deleteRights(scope.row,scope.row.id, item1.id)"
                        >{{ item1.authName }}</el-tag
                      >
                    </el-col>
                    <!-- 第二列 -->
                    <el-col :span="20">
                      <el-row v-for="item2 in item1.children" :key="item2.id">
                        <el-col :span="6">
                          <el-tag
                            type="danger"
                            closable
                            @close="deleteRights(scope.row,scope.row.id, item2.id)"
                            >{{ item2.authName }}</el-tag
                          >
                        </el-col>
                        <el-col :span="18">
                          <el-tag
                            type="warning"
                            closable
                            @close="deleteRights(scope.row,scope.row.id, item3.id)"
                            v-for="item3 in item2.children"
                            :key="item3.id"
                            >{{ item3.authName }}</el-tag
                          >
                        </el-col>
                      </el-row>
                    </el-col>
                  </el-row>
                </div>
                <div v-else>没有权限</div>
              </div>
            </template>
    </el-table-column>
    <el-table-column type="index" label="#"> </el-table-column>
    <el-table-column  prop="roleName"  label="角色名称"> </el-table-column>
    <el-table-column  prop="roleDesc" label="角色描述"> </el-table-column>
    <el-table-column  label="操作">
         <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="bianji(scope.row)">编辑</el-button>
              <el-button type="danger"  size="mini" @click="del(scope.row.id)">删除</el-button>
              <el-button type="warning" size="mini"
               @click="setRoleTo(scope.row)"
              >设置</el-button>
         </template>
    </el-table-column>
  </el-table>
</el-card>

<!-- 添加角色的模态框 -->
<el-dialog title="添加角色" :visible.sync="dialogFormVisible">
  <el-form :model="form" :rules="rules" ref="formName">
    <el-form-item label="角色名称" prop="roleName">
      <el-input v-model="form.roleName"></el-input>
    </el-form-item>
    <el-form-item label="角色描述" prop="roleDesc">
      <el-input v-model="form.roleDesc"></el-input>
    </el-form-item>
    
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="addroles">确 定</el-button>
  </div>
</el-dialog>

<!-- 编辑角色的模态框 -->
<el-dialog title="修改角色" :visible.sync="dialogFormVisibles">
  <el-form :model="forms" :rules="rules" ref="formName">
    <el-form-item label="角色名称" prop="roleName">
      <el-input v-model="forms.roleName"></el-input>
    </el-form-item>
    <el-form-item label="角色描述" prop="roleDesc">
      <el-input v-model="forms.roleDesc"></el-input>
    </el-form-item>
    
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisibles = false">取 消</el-button>
    <el-button type="primary" @click="tijiao">确 定</el-button>
  </div>
</el-dialog>

<!-- 分配角色的对话框 -->
 <el-dialog
      title="提示"
      :visible.sync="dialogVisiblesss"
      width="30%"
    >
      <el-tree
        ref="treelist"
        :data="treeData"
        show-checkbox
        node-key="id"
        :default-checked-keys="checkall"
        default-expand-all
        :props="defaultProps"
      >
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisiblesss = false">取 消</el-button>
        <el-button type="primary" @click="addsetRoleTo">确 定</el-button>
      </span>
    </el-dialog>





    </div>
</template>

<script>
import {
  getrolesApi,
  addrolesApi,
  delrolesApi,
  bjrolesApi,
  getrightsApi,
  shanjueseApi,
  xiurolesApi
} from "@/http/api";
export default {
  data() {
    return {
      tableData: [], //表格的信息
      dialogFormVisible: false, //添加角色的模态框
      form: {
        //添加角色的列表
        roleName: "",
        roleDesc: ""
      },
      forms: {
        //修改角色的列表
        roleName: "",
        roleDesc: ""
      },
      dialogFormVisibles: false, //修改的模态框
      rules: {
        roleName: [
          { required: true, message: "请输入活动名称", trigger: "blur" }
        ],
        roleDesc: [
          { required: true, message: "请输入活动名称", trigger: "blur" }
        ]
      },
      dialogVisiblesss: false, //分配角色的对话框
      treeData: [], //树状图打开的默认状态
      defaultProps: {
        label: "authName",
        children: "children"
      },
      checkall: [],
      list:[] ,
      roleid:0,
    };
  },
  mounted() {
    this.getlist();
  },
  methods: {
    //角色列表
    async getlist() {
      let res = await getrolesApi();
      console.log(res);
      this.tableData = res;
    },
    //点击添加角色出现模态框
    add() {
      this.dialogFormVisible = true;
    },
    //点击确定添加内容
    addroles() {
      this.$refs.formName.validate(async valid => {
        if (!valid) {
          return false;
        } else {
          //请求接口
          let res = await addrolesApi(this.form);
          this.dialogFormVisible = false;
          this.getlist();
        }
      });
    },
    //删除
    del(id) {
      console.log("删除的id", id);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          await delrolesApi(id);
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.getlist();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //编辑
    bianji(row) {
      console.log(row);
      this.forms = row;
      this.dialogFormVisibles = true;
    },
    //提交编辑
    async tijiao() {
      console.log(this.forms);
      let { id, roleName, roleDesc } = this.forms; //先解构赋值 在调用接口
      await bjrolesApi(id, this.forms);
      this.dialogFormVisibles = false;
      this.getlist();

      //  let {id} =res.id
      //  let {roleName}=res.roleName
      // console.log("编辑内容",res)
      // this.forms=res
    },
    //打开分配权限对话框
    async setRoleTo(row) {
      this.checkall=[]
      console.log("row", row);
      this.dialogVisiblesss = true;
      //所有权限列表的接口
      let res = await getrightsApi("tree");
      this.treeData = res;
      console.log(this.treeData);
      this.roleid = row.id;
      this.getchecked(row, this.checkall); // row当前点击的数据
      console.log("当前点击的", row);
      console.log("角色拥有的权限", this.checkall);
    },
    getchecked(row, checkall) {
      if (!row.children) {
        return checkall.push(row.id);
      }
      row.children.forEach(item => {
        this.getchecked(item, checkall);
      });
    },
    //点击确定时关闭模态框
    async addsetRoleTo() {
      this.dialogVisiblesss = false;
      const allcheck = this.$refs.treelist.getCheckedKeys(); //全选节点
      const bancheck = this.$refs.treelist.getHalfCheckedKeys(); //半选节点
      const hebing = [...allcheck, ...bancheck]; //进行合并
      const arr = hebing.join(","); //以逗号分割
      //获取角色列表的接口
      const res = await xiurolesApi(this.roleid, arr);
      this.getlist();
    },
    async getlists() {
      //获取编辑角色列表的接口
      let res = await bjrolesApi();
      this.list = res;
      console.log("this.list", this.list[1].id);
      // console.log(res)
    },
    //删除角色指定权限   getrolelist 
    async deleteRights(row, roleid, rightid) {
      console.log(roleid, rightid);
      let res = await shanjueseApi(roleid, rightid);
      console.log("res", res);
      row.children = res;
      // this.getlist();
    }
  }
};
</script>

<style scoped >
.role_box {
  padding: 20px;
}
.row_box1 {
  margin: 10px;
}
.el-tag {
  margin: 13px;
}
</style>
